package com.finfosoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.finfosoft.bean.User;
import com.finfosoft.repository.UserJpaRepsitory;

@RestController
public class DbController {

	@Autowired
	private UserJpaRepsitory userJpaRepsitory;
	
	@RequestMapping("/getUsers")
	public List<User> findByNameLike (){
		return userJpaRepsitory.findAll();
	}
	
	
	@RequestMapping("/getUser")
	public List<User> findByLikeName (String name){
		
		System.out.println("name = " + name);
		return userJpaRepsitory.findByNameLike(name);
	}
	
	@RequestMapping("/deleteUser")
	public void deleteById (int id){
		System.out.println("id = " + id);
		userJpaRepsitory.delete(id);
	}
	@RequestMapping("/saveUser")
	public void deleteById (User user){
		user.setName("xiaoluobo");
		userJpaRepsitory.save(user);
	}
	
	@RequestMapping("/page")
	public Page<User> page(){
		Page<User> userPage = userJpaRepsitory.findAll(new PageRequest(1, 2));
		return userPage;
	}
	
	
	@RequestMapping("/sort")
	public List<User> sort(){
		List<User> list = userJpaRepsitory.findAll(new Sort(Direction.ASC,"age"));
		return list;
	}
	
	@RequestMapping("/userByNa")
	public User getByNameAndAge(String name, int age){
		System.out.println("name = " + name);
		System.out.println("age = " + age);
		return userJpaRepsitory.findByNameAndAge(name, age);
	}
	
}
